package de.uni.kassel.campusappver3.view;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import de.uni.kassel.campusappver3.R;
import de.uni.kassel.campusappver3.model.Branch;
import de.uni.kassel.campusappver3.model.Resources;

public class ListActivity extends Activity {

	private ListView lv;
	public HashMap<String, String> postitonKey = new HashMap<String, String>();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);

		lv = (ListView) findViewById(R.id.list);

		//HashMap with all branches
		HashMap<String, Branch> branches = Resources.getInstance().getBranches();
		
		//actual Strings added to the ListView
		LinkedList<String> values = new LinkedList<String>();

		//helper list to detected duplicates
		LinkedList<String> duplicates = new LinkedList<String>();
		
		//add all branches to values
		for (Branch branch : branches.values()) {
			if (!duplicates.contains(branch.getName())) {
				String item = branch.getName() + "\n \n" + branch.getProfName();
				values.add(item);
				postitonKey.put(item, branch.getKey());
				duplicates.push(branch.getName());
			}
		}
			
		//alphabetical sort
		Collections.sort(values);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, values);
		
		lv.setAdapter(adapter);

		//if item is clicked, a new BranchActiviy is created
		//also putting the key into the bundle to know which branch
		//has need clicked
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> a, View v, int position, long id) {
				Object item = lv.getItemAtPosition(position);
				Intent newActivity = new Intent(getApplicationContext(), BranchesActivity.class);
				newActivity.putExtra("Fachgebiet", postitonKey.get(item.toString()));
				startActivity(newActivity);
			}
		});
	}
}
